import {connect} from 'react-redux';
import Taro, {useDidShow} from '@tarojs/taro';
import {Image, View} from "@tarojs/components";
import './index.less'

function Index(props){
  const {selected, dispatch} = props;
  // const [selected, setSelected] = useState(0);
  useDidShow(() => {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2 //0,1，2 0-导航一  1-导航二  2-个人中心
      })
    }
    // if (typeof Taro.getCurrentInstance().page.getTabBar() === 'function' && Taro.getCurrentInstance().page.getTabBar()) {
    //   console.log('Taro.getCurrentInstance().page.getTabBar()', Taro.getCurrentInstance().page.getTabBar())
    //   Taro.getCurrentInstance().page.getTabBar().setData({
    //     selected: 2 //0,1，2 0-导航一  1-导航二  2-个人中心
    //   })
    // }
  })
  const state = {
    color: '#000000',
    selectedColor: '#237AF5',
    list: [
      {
        pagePath: '/pages/index/index',
        selectedIconPath: '../assets/homeActive.png',
        iconPath: '../assets/home.png',
        text: '首页'
      },
      {
        pagePath: undefined,
        iconPath: '../assets/scan.png',
        selectedIconPath: '../assets/scan.png',
        text: ''
      },
      {
        pagePath: '/pages/mine/index',
        selectedIconPath: '../assets/meActive.png',
        iconPath: '../assets/me.png',
        text: '我的'
      }
    ]
  }
  function  switchTab (index, url) {
    if(index===1){   //如果点击扫一扫,就调取扫一扫功能
      Taro.scanCode({
        success: (res) => {
          console.log('扫一扫成功的结果返回', res);
          // let paramStr = '';
          // let pileCode = '';
          // let gunCode = '';
          // if (res.path) {
          //   if (res.result) {  // 普通二维码
          //     const paramStr = unescape(res?.result).split('code=').length > 1 ? unescape(res?.result).split('code=')[1] : '';
          //     const pileCode = paramStr.slice(0, -2);
          //     let gunCode = paramStr.slice(-2);
          //     if (gunCode.startsWith('0')) gunCode = gunCode.slice(-1);
          //     console.log('pileCode普通', pileCode);
          //     console.log('gunCode普通', gunCode);
          //     //扫码成功后的操作
          //     if (pileCode && gunCode){
          //       dispatch({
          //         type: 'stations/jumpToPile',
          //         payload: {
          //           pileCode,
          //           gunCode
          //           // pileId
          //         }
          //       });
          //       setTimeout(() => Taro.reLaunch({url: '/moduleA/pages/pile/index'}), 300)
          //     }
          //   }
          // } else {
          //   Taro.showToast({
          //     title: '无法识别该设备,请使用微信扫一扫识别',
          //     duration: 2000,
          //     icon: 'none'
          //   })
          // }
          // else if (res.path) {  // 小程序码
          //   const  paramStr = unescape(res?.path).split('scene=').length  > 1 ? unescape(res?.path).split('scene=')[1] : '';
          //   const pileStr = paramStr.split('&').length > 1 ? paramStr.split('&')[0] : ''
          //   const gunStr = paramStr.split('&').length > 1 ? paramStr.split('&')[1] : ''
          //   console.log('paramStr', paramStr);
          //   console.log('pileStr', pileStr);
          //   console.log('gunStr', gunStr);
          //   const pileCode = pileStr.split('=').length >1 ? pileStr.split('=')[1] : '';
          //   const gunCode = gunStr.split('=').length >1 ? gunStr.split('=')[1] : '';
          //   console.log('pileCode葵花', pileCode);
          //   console.log('gunCode葵花', gunCode);
          //   //扫码成功后的操作
          //   if (pileCode && gunCode){
          //     dispatch({
          //       type: 'stations/jumpToPile',
          //       payload: {
          //         pileCode,
          //         gunCode
          //         // pileId
          //       }
          //     });
          //     setTimeout(() => Taro.reLaunch({url: '/moduleA/pages/pile/index'}), 300)
          //   }
          // }
          // const pathStr = res?.path ? unescape(res?.path) :  unescape(res?.path);
          // const paramStr = pathStr.length ? pathStr.split('scene=').length > 1 ? pathStr.split('scene=')[1] : '' : '';
          // const pileStr = paramStr.split('&').length > 1 ? paramStr.split('&')[0] : ''
          // const gunStr = paramStr.split('&').length > 1 ? paramStr.split('&')[1] : ''
          // const pileCode = pileStr.split('=').length >1 ? pileStr.split('=')[1] : '';
          // const gunCode = gunStr.split('=').length >1 ? gunStr.split('=')[1] : '';
          if (res.result) {  // 普通二维码
            const paramStr = unescape(res?.result).split('code=').length > 1 ? unescape(res?.result).split('code=')[1] : '';
            const pileCode = paramStr.slice(0, -2);
            let gunCode = paramStr.slice(-2);
            if (gunCode.startsWith('0')) gunCode = gunCode.slice(-1);
            console.log('pileCode普通', pileCode);
            console.log('gunCode普通', gunCode);
            //扫码成功后的操作
            if (pileCode && gunCode){
              dispatch({
                type: 'stations/jumpToPile',
                payload: {
                  pileCode,
                  gunCode
                  // pileId
                }
              });
              setTimeout(() => Taro.reLaunch({url: '/moduleA/pages/pile/index'}), 300)
            }
          } else if (res.path) {  // 小程序码
            const  paramStr = unescape(res?.path).split('scene=').length  > 1 ? unescape(res?.path).split('scene=')[1] : '';
            const pileStr = paramStr.split('&').length > 1 ? paramStr.split('&')[0] : ''
            const gunStr = paramStr.split('&').length > 1 ? paramStr.split('&')[1] : ''
            console.log('paramStr', paramStr);
            console.log('pileStr', pileStr);
            console.log('gunStr', gunStr);
            const pileCode = pileStr.split('=').length >1 ? pileStr.split('=')[1] : '';
            const gunCode = gunStr.split('=').length >1 ? gunStr.split('=')[1] : '';
            console.log('pileCode葵花', pileCode);
            console.log('gunCode葵花', gunCode);
            //扫码成功后的操作
            if (pileCode && gunCode){
              dispatch({
                type: 'stations/jumpToPile',
                payload: {
                  pileCode,
                  gunCode
                  // pileId
                }
              });
              setTimeout(() => Taro.reLaunch({url: '/moduleA/pages/pile/index'}), 300)
            }
          }
        },
        fail:(res) =>{
          console.log('扫一扫失败的结果返回', res);
      }
      })
    }else{
      // setSelected(index)
      dispatch({
        type:'app/upState',
        payload: {
          selected: index
        }
      })
      if (url) Taro.switchTab({ url })
    }
  }
  return (
    <View className='tab-bar'>
      <View className='tab-bar-border'></View>
      {state.list.map((item, index) => {
        return (
          <View
            key={index}
            className='tab-bar-item'
            onClick={() => switchTab(index, item.pagePath)}
          >
            <Image src={selected === index ? item.selectedIconPath : item.iconPath} className={index === 1 ? 'scanBtn' : ''} />
            <View style={{ color: selected === index ? state.selectedColor : state.color }}>{item.text}</View>
          </View>
        )
      })}
    </View>
  )
}
export default connect(({app: {selected}}) => {return {selected}})(Index)
